/*
 * @Date: 2022-05-23 11:22:53
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-05-23 14:56:25
 * @FilePath: \my-react-components\src\hooks\useClickOutSide.ts
 * @Description: something
 */
import React, { RefObject, useEffect } from "react";
const useClickOutSide = (ref: RefObject<HTMLElement>, handle: Function) => {
  useEffect(() => {
    const listener = (e: MouseEvent) => {
      if (!ref.current || ref.current.contains(e.target as HTMLElement)) {
        return;
      }
      handle(e);
    };
    document.addEventListener("click", listener);
    return () => {
      document.removeEventListener("click", listener);
    };
  }, [ref, handle]);
};
export default useClickOutSide;
